/**
 * Author: w景洋.
 * Time: 2016/4/19 0019
 */
angular.module('service.shopmenu', [
    "ngStorage",
])
    //获取店铺菜单
    .service('ShopMenuService',['$localStorage','MenuGoodsService',function($localStorage,MenuGoodsService){
        var $storage = $localStorage.$default({
            cmtShopcart: {},
        });
        var shop_goods_list = $storage.cmtShopcart;
        var menuList = {};
        //根据店铺id获取店铺菜单
        this.getMenuByShopid = function(shopid){
            if(menuList[shopid]){
                return menuList[shopid];
            }
            //
            var category_goodsid_map = {};
            if (shop_goods_list[shopid]) {
                var shopcart_goods_list = shop_goods_list[shopid];
                for (var i=0; i<shopcart_goods_list.length; i++) {
                    var goodsitem = shopcart_goods_list[i];
                    var goodsid_map = {};
                    for (var j=0; j<goodsitem.goods.length; j++) {
                        var goods = goodsitem.goods[j];
                        goodsid_map[goods.goodsid] = goods;
                    }
                    category_goodsid_map[goodsitem.cid] = goodsid_map;
                }
            }
            //
            var menu = MenuGoodsService.getMenuByShopid(shopid);
            menu.$promise.then(function(goodsList){
                for(var i=0;i<goodsList.length;i++){
                    var goodsitem = goodsList[i];
                    var goodsid_map = category_goodsid_map[goodsitem.cid];
                    for(var j=0;j<goodsitem.goods.length;j++){
                        var good = goodsitem.goods[j];
                        if (goodsid_map && goodsid_map[good.goodsid]) {
                            good.buycount = goodsid_map[good.goodsid].buycount;
                        } else {
                            good.buycount = 0;
                        }
                    }
                }
            });
            menuList[shopid] = menu;
            return menu;
        }

        //新增购买
        this.addGood = function(shopid,cid,goodsid){
            //console.log('addGood('+shopid+','+goodsid+')');
            var goodsList = menuList[shopid];
            if(goodsList == undefined){
                goodsList = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i = 0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                if(goodsitem.cid == cid){
                    break;
                }
            }
            for(var j=0; j<goodsitem.goods.length;j++){
                var good = goodsitem.goods[j];
                if(good.goodsid == goodsid){
                    good.buycount++;
                    break;
                }
            }
        }

        //减少购买
        this.removeGood = function(shopid,cid,goodsid){
            //console.log('removeGood('+shopid+','+goodsid+')');
            var goodsList = menuList[shopid];
            if(goodsList == undefined){
                goodsList = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i=0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                if(goodsitem.cid == cid){
                    break;
                }
            }
            for(var j=0;j<goodsitem.goods.length;j++){
                var good = goodsitem.goods[j];
                if(good.goodsid == goodsid){
                    good.buycount--;
                    break;
                }
            }
        }

        //输入框失去焦点
        this.inputBlur = function(shopid,cid,goodsid){
            var goodsList = menuList[shopid];
            var goodsitem = null;
            for(var i=0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                if(goodsitem.cid == cid){
                    break;
                }
            }
            for(var j=0;j<goodsitem.goods.length;j++){
                var good = goodsitem.goods[j];
                if(good.goodsid == goodsid){
                    if(good.buycount == null){
                        input_alert('输入有误')
                        good.buycount=0
                        break;
                    }else if(good.buycount > 0){
                    }
                }
            }
        }

        //监听输入框不规范输入
        this.inputChange = function(shopid,cid,goodsid){
            var goodsList = menuList[shopid];
            var goodsitem = null;
            for(var i=0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                if(goodsitem.cid == cid){
                    break;
                }
            }
            for(var j=0;j<goodsitem.goods.length;j++){
                var good = goodsitem.goods[j];
                if(good.goodsid == goodsid){
                    if(good.buycount < 0){
                        input_alert('超出购买范围')
                        good.buycount = 0
                        break;
                    }
                    else if(good.buycount > 999){
                        input_alert('超出购买范围')
                        good.buycount = 999
                        break;
                    }
                }
            }
        }

        //移除某个id的商品
        this.cleanGoodById = function(shopid,cataid,goodsid){
            //console.log('cleanGoodById('+shopid+','+goodsid+')');
            var goodsList = menuList[shopid];
            if(goodsList == undefined){
                goodsList = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i=0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                if(goodsitem.cid == cataid){
                    break;
                }
            }
            for(var j=0;j<goodsitem.goods.length;j++){
                var good = goodsitem.goods[j];
                if(good.goodsid == goodsid){
                    good.buycount = 0;
                    break;
                }
            }
        }

        //移除某个商店所有的商品信息
        this.removeShopById =function(shopid){
            var goodsList = menuList[shopid];
            if(goodsList == undefined){
                goodsList = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i=0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                for(var j=0;j<goodsitem.goods.length;j++){
                    var good = goodsitem.goods[j];
                    good.buycount = 0;
                }
            }
        }

        //获取商品详情
        this.getGoodsDetail = function(shopid,cid,goodsid){
            //console.log('gooddetail('+shopid+','+goodsid+')');
            var goodsList = menuList[shopid];
            if(goodsList == undefined){
                goodsList = shop_goods_list[shopid];
            }
            var goodsitem = null;
            for(var i=0;i<goodsList.length;i++){
                goodsitem = goodsList[i];
                if(goodsitem.cid == cid){
                    break;
                }
            }
            var good = null;
            for(var j=0;j<goodsitem.goods.length;j++){
                good = goodsitem.goods[j];
                if(good.goodsid == goodsid){
                    break;
                }
            }
            return {cid:cid,name:goodsitem.name,goods:good};
        }

    }])
